package utils;

import java.io.File;

public class FileBean implements Comparable<FileBean> {
    private static final long BIG_FILE = 100 * 1024 * 1024; //100M则认为是大文件
    private File file;
    private String parent;
    private String name;
    private String path;
    private long size;

    //用于FileDeleter的参数
    private String key;
    private String md5;
    private boolean target;

    public boolean target() {
        return target;
    }

    public String key() {
        return key;
    }

    public String reKey() {
        if (file != null) {
            if (TextUtils.isEmpty(md5)) {
                md5 = MD5Util.parse(path);
            }
            return md5;
        } else {
            return null;
        }
    }

    public String md5() {
        return md5;
    }

    public String path() {
        return path;
    }

    public String parent() {
        return parent;
    }

    public String name() {
        return name;
    }

    //大于100M的文件，认为是大文件
    public boolean bigFile() {
        return size >= BIG_FILE;
    }

    public String bigKey() {
        return String.format("%s-%s", size, name);
    }

    public void del(OnLogListener listener) {
        if (file != null && file.exists()) {
            listener.log(String.format("删除的文件：【%s】,%s", (file.delete() ? "成功" : "失败"), path));
            this.file = null;
        }
    }

    public void move(String toPathTemp, OnLogListener listener) {
        File file = new File(toPathTemp);
        String toPath;
        String toName;
        String rawName = this.name;
        if (file.isDirectory()) {//仅仅是移动文件夹
            toPath = toPathTemp;
            toName = rawName;
        } else {//既移动文件夹，又改名字
            toPath = file.getParentFile().getPath();
            toName = file.getName();
        }

        int index = 0;
        while (true) {
            File toFile = new File(toPath, toName);
            if (toFile.exists()) {
                index++;
                toName = fixedName(index, toName);
                if (listener != null) {
                    listener.log(String.format("%s 文件在目标文件夹有同名文件,匹配新的命名 【%s】 ", path, toName));
                }
            } else {
                boolean success = this.file.renameTo(toFile);
                if (listener != null) {
                    listener.log(String.format("%s 文件移动结果: %b，名字为【%s】", path, (success ? "成功" : "失败"), toName));
                }
                break;
            }
        }
    }

    private String fixedName(int index, String name) {
        if (!TextUtils.isEmpty(name) && name.contains(".")) {
            String[] split = name.split("\\.");
            String realName = split[0];
            String type = split[1];
            String fixedName = realName;
            if (hasIndex(name)) {
                int start = name.lastIndexOf(FIX_START);
                try {
                    fixedName = name.substring(0, start);
                } catch (Exception ex) {//如果报错，就走普通的逻辑
                    LogUtil.log(ex.getMessage());
                }
            }
            return String.format("%s%s%d%s.%s", fixedName, FIX_START, index, FIX_END, type);
        }
        return name;
    }

    private static final String FIX_START = "-(";
    private static final String FIX_END = ")";

    private boolean hasIndex(String name) {
        if (name.contains(FIX_START) && name.contains(FIX_END)) {
            int start = name.lastIndexOf(FIX_START);
            int end = name.lastIndexOf(FIX_END);
            int point = name.lastIndexOf(".");
            return start < end && (end + 1 == point);
        }
        return false;
    }

    public static FileBean newInstance(File file) {
        FileBean bean = new FileBean();
        bean.file = file;
        bean.parent = file.getParentFile().getAbsolutePath();
        bean.path = file.getAbsolutePath();
        bean.name = file.getName();
        bean.size = file.length();
        bean.key = String.valueOf(bean.size);
        return bean;
    }

    public static FileBean newInstance(boolean target, File file) {//改为用文件的size来进行校验文件的一致性,如果size一致,再比较md5
        FileBean bean = newInstance(file);
        bean.target = target;
        return bean;
    }

    @Override
    public int compareTo(FileBean second) {
        try {
            int delta = name.length() - second.name.length();
            if (delta == 0) {//如果名字相同,则文件夹短的为准,文件夹深的就删除
                return path.length() - second.path.length();
            } else {
                return delta;
            }
        } catch (Exception ex) {
            return 0;
        }
    }
}
